package cn.shadow.module.login;

import cn.shadow.frame.game.MyGame;
import cn.shadow.frame.screen.MyScreen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;

/**
 * 登录场景，先做一个登录的界面出来
 */
public class LoginScreen extends MyScreen {

    /**
     * 登录界面
     */
    private Image loginImage;

    private Stage loginStage;

    private Button loginButton;

    // 文本框背景纹理
    private Texture bgTexture;

    // 文本框中的光标纹理
    private Texture cursorTexture;

    // 位图字体
    private BitmapFont bitmapFont;

    // 文本框（用户名）
    private TextField username;

    // 文本框（密码）
    private TextField password;

    // 文本框的宽高
    public static final int TEXT_FIELD_WIDTH = 300;
    public static final int TEXT_FIELD_HEIGHT = 50;

    AssetManager assetManager = new AssetManager();
    public LoginScreen(MyGame myGame) {
        super(myGame);
        initTextFiled();
//        assetManager.load("image/login.jpg", Texture.class);
    }

    public void initTextFiled() {
        bgTexture= new Texture(Gdx.files.internal("image/login.jpg"));
        cursorTexture= new Texture(Gdx.files.internal("image/login.jpg"));
        loginImage=new Image(new Texture(Gdx.files.internal("image/login.jpg")));
        /*
         * 第 2 步: 创建 TextFieldStyle
         */
        TextField.TextFieldStyle style = new TextField.TextFieldStyle();

        // 设置背景纹理区域
        style.background = new TextureRegionDrawable(new TextureRegion(bgTexture,0,0));

        // 设置光标纹理区域
//        style.cursor = new TextureRegionDrawable(new TextureRegion(createCursorTexture()));

        bitmapFont = new BitmapFont();
        // 设置文本框显示文本的字体来源
        style.font = bitmapFont;

        // 设置文本框字体颜色为白色
        style.fontColor = new Color(1, 1, 1, 1);

        /*
         * 第 3 步: 创建 TextField
         */
        username = new TextField("请输入用户名", style);
        password = new TextField("请输入密码", style);


        // 设置文本框的宽高
        username.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);
        password.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);

        // 设置文本框的位置
        username.setPosition(90, 500);
        password.setPosition(90, 430);

        // 文本框中的文字居中对齐
        username.setAlignment(Align.center);
        password.setAlignment(Align.center);

        // 用于显示密码的文本框, 需要将文本框设置为密码模式
        password.setPasswordMode(true);
        // 显示密码时用 * 号代替密码字符
        password.setPasswordCharacter('*');

        String logBtnUp = "image/btn_up.png";
        String logBtnDown = "image/btn_up.png";


        loginStage = new Stage();
        loginButton = new MyButton(logBtnUp,logBtnDown);
        loginButton.setPosition(300,200);
        /*
         * 第 4 步: 添加 TextField 到舞台
         */
        loginStage.addActor(username);
        loginStage.addActor(password);
        loginStage.addActor(loginButton);
        loginStage.addActor(loginImage);
    }

    @Override
    public Stage getControlStage() {
        return loginStage;
    }

/*    private Texture createCursorTexture() {
        Pixmap pixmap = new Pixmap(1, TEXT_FIELD_HEIGHT - 4, Pixmap.Format.RGBA8888);
        pixmap.setColor(1, 0, 0, 1);
        pixmap.fill();
        Texture texture = new Texture(pixmap);
        pixmap.dispose();
        return texture;
    }*/

    @Override
    protected void addLister(Stage controlStage) {
        loginButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                System.out.println(String.format("userName=%s,password=%s", username.getText(), password.getText()));
            }
        });
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        Gdx.gl.glClearColor(0.75F, 1, 0.98F, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        loginStage.act();
        loginStage.draw();
    }

    public Stage getLoginStage() {
        return loginStage;
    }
}
